from apps.education.course.baseViews import *
from apps.education.courseStudent.dao import CourseStudentDao
from apps.education.student.dao import StudentDao
from apps.education.teacher.dao import TeacherDao

class CourseForCViewSet(BaseCourseViewSet):
    authentication_classes = []
    permission_classes = []
    def get_queryset(self):
        params = getRequestParams(self.request)
        _list = CourseDao().findAll()
        return _list
    
class CourseViewSet(BaseCourseViewSet):
    def get_queryset(self):
        params = getRequestParams(self.request)
        _list = CourseDao().findAll()
        return _list

    # 报名课程
    @action(methods=['post'], detail=False)
    def signUp(self, request):
        params = getRequestParams(request)
        obj_id = params.get('obj_id')
        if not obj_id:
            return Response(response_message('课程id参数必填'))
        course = CourseDao().findOneByObjId(obj_id)
        if course.status == 1:
            return Response(response_message('草稿状态不能报名'))
        if timezone.now() > course.lastTime:
            return Response(response_message('报名已截止'))
        teacher = TeacherDao().findOneByFilters({'user': request.user.id})
        if teacher:
            return Response(response_message('学生身份才能报名'))
        student = StudentDao().findOneByFilters({'user': request.user.id})
        if not student:
            return Response(response_message('请先完善身份认证'))
        if student.checkStatus != 'PASS':
            return Response(response_message('认证资料未审核通过'))
        courseStudents = CourseStudentDao().findAll().filter(course=course, student__user=request.user)
        if courseStudents.exists():
            return Response(response_message('您已报名，请勿重复报名'))
        CourseStudentDao().create({
            'course': course.id,
            'student': student.id
        })
        return Response(response_success('报名成功'))